<?php 
    class Order_model extends CI_Model{

        function __construct(){
            parent::__construct();
        }

        function get_order_list($limit,$offset,$conditions = array()){
            $where = " 1=1 ";//" order_type = $type ";
            if(!empty($conditions['school_id'])){
                $where .= " and school_id='{$conditions['school_id']}' ";
            }
            if(isset($conditions['order_status']) && $conditions['order_status'] != 9){
                $where .= " and order_status='{$conditions['order_status']}' ";
            }
            if(!empty($conditions['start_time'])){
                $start_time = strtotime($conditions['start_time']);
                $where .= " and o.order_time >= '{$start_time}' ";
            }
            if(!empty($conditions['end_time'])){
                $end_time = strtotime($conditions['end_time']) + 3600 * 24;
                $where .= " and o.order_time <= '{$end_time}' ";
            }
            $sql = "SELECT SQL_CALC_FOUND_ROWS o.*,s.school_name FROM {$this->db->dbprefix}shopping_orders o left join {$this->db->dbprefix}o2o_school s on s.id = o.school_id where $where order by order_id desc limit $limit,$offset";
            $result = $this->db->query($sql)->result_array();
            $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
            return array("count" => $row_num['row_num'], "result" => $result);
        }

        function get_order_total_amount($conditions = array()){
            $where = " 1 = 1 ";//" order_type = $type ";
            if(!empty($conditions['school_id'])){
                $where .= " and school_id='{$conditions['school_id']}' ";
            }
            if(isset($conditions['order_status']) && $conditions['order_status'] != 9){
                $where .= " and order_status='{$conditions['order_status']}' ";
            }
            if(!empty($conditions['start_time'])){
                $start_time = strtotime($conditions['start_time']);
                $where .= " and order_time >= '{$start_time}' ";
            }
            if(!empty($conditions['end_time'])){
                $end_time = strtotime($conditions['end_time']) + 3600 * 24;
                $where .= " and order_time <= '{$end_time}' ";
            }
            $sql = "SELECT sum(total_order_amount) AS total_amount FROM {$this->db->dbprefix}shopping_orders where $where";
            $result = $this->db->query($sql)->row_array();
            return $result;
        }
    }